<template>
  <div class="search">
    <f-form ref="form">
      <f-form-item label="药品" name="drugsName" :value="formItem.drugsName" :disabled="true"/>
      <f-form-item label="成本价格" name="costPrice" :value="formItem.costPrice" :disabled="true"/>
      <f-form-item label="采购日期" name="planDate" :value="formItem.planDate" :disabled="true"/>
      <a-divider>采购批号，如有多个，请添加多个</a-divider>
      <f-form-item type="other" label="批号">
        <template v-for="(item,index) in options">
          <a-input placeholder="入库批号" style="width: 30%;margin-left: 5px" v-model="item.drugsLotNo"></a-input>
          <a-input placeholder="入库数量" style="width: 10%;margin-left: 5px" v-model="item.drugsNum"></a-input>
          <a-icon class="dynamic-delete-button" type="minus-circle-o" @click="() => remove(index)" v-if="index>0"/>
          <br>
        </template>
      </f-form-item>
      <f-form-item type="other" style="width: 100%;padding-left: 150px">
        <a-button type="dashed" @click="add">
          <a-icon type="plus"/>
          添加批号
        </a-button>
      </f-form-item>
    </f-form>
  </div>
</template>
<script>
  import api from './js/api'

  export default {
    name: 'drugslotForm',
    props: {},
    inject: ["drugsplan"],
    data() {
      return {
        refs: this.$refs,
        api: Object.assign(api, this.api),
        isAdd: true,//是否是添加模式
        loaded: false,//是否加载完成
        options: [{
          drugsLotNo: '',
          drugsNum: ''
        }],
        drugsPlanInfo: {},
        formItem: this.getFormItem()
      }
    },
    methods: {
      getFormItem() {
        return {
          drugsLotId: '',
          drugsLotId2: '',
          drugsLotNo: '',
          drugsPrice: '',
          drugsId: '',
          costPrice: '',
          drugsNum: '',
          drugsName: '',
          syCount: '',
          planDate: '',
          createTime: '',
          updateTime: ''
        }
      },
      add() {
        this.options.push({
          drugsLotNo: '',
          drugsNum: ''
        })
      },
      remove(index) {
        this.options.splice(index, 1)
      },
      //表单赋值
      setData(data) {
        this.$refs.form.resetFields()
        this.formItem = this.getFormItem()
        this.formItem = data
        this.drugsPlanInfo = this.formItem
        this.loaded = true
      },
      //提交表单
      handleSubmit(callback) {
        let that=this;
        this.$refs.form.getForm().validateFields((valid, values) => {
            if (!valid) {
              this.http.post(this, {
                url: this.api.drugsPlan.batchSaveDrugsLot,
                data: {
                  drugsLotInfo: JSON.stringify(this.options),
                  drugsPlanId:this.drugsPlanInfo.drugsPlanId,
                  drugsId: this.drugsPlanInfo.drugsId,
                  costPrice: this.drugsPlanInfo.costPrice
                },
                success: function(me) {
                  that.drugsplan.refresh(true)
                }
              })
            }
          }
        )
      }
    },
    created() {
    },
    components: {}
  }
</script>

<style scoped lang="less" rel="stylesheet/less">

</style>
